using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows.Forms;
using System.Data;
using System.Drawing;
using System.Reflection;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors;
using THSLib;

namespace THSERP.BaseProvider
{
    public partial class THSComboBox : DevExpress.XtraEditors.ComboBoxEdit
    {
        #region Variables
        protected String _THSFieldGroup;
        protected String _THSFieldRelation;
        protected THSERPScreen _screen;
        protected String _THSComment;
        protected String _THSError;
        protected String _THSDataSource;
        protected String _THSDataMember;
        protected String _THSPropertyName;
        protected String _THSPrivilege;
        #endregion

        #region Public Properties
        public THSERPScreen Screen
        {
            get { return _screen; }
            set { _screen = value; }
        }

        [Category("THS")]
        public String THSFieldGroup
        {
            get
            {
                return _THSFieldGroup;
            }
            set
            {
                _THSFieldGroup = value;
            }
        }

        [Category("THS")]
        public String THSFieldRelation
        {
            get
            {
                return _THSFieldRelation;
            }
            set
            {
                _THSFieldRelation = value;
            }
        }

        [Category("THS")]
        public String THSComment
        {
            get
            {
                return _THSComment;
            }
            set
            {
                _THSComment = value;
            }
        }

        [Category("THS")]
        public String THSError
        {
            get
            {
                return _THSError;
            }
            set
            {
                _THSError = value;
            }
        }

        [Category("THS")]
        public String THSDataSource
        {
            get
            {
                return _THSDataSource;
            }
            set
            {
                _THSDataSource = value;
            }
        }

        [Category("THS")]
        public String THSDataMember
        {
            get
            {
                return _THSDataMember;
            }
            set
            {
                _THSDataMember = value;
            }
        }

        [Category("THS")]
        public String THSPropertyName
        {
            get
            {
                return _THSPropertyName;
            }
            set
            {
                _THSPropertyName = value;
            }
        }

        [Category("THS")]
        public String THSPrivilege
        {
            get
            {
                return _THSPrivilege;
            }
            set
            {
                _THSPrivilege = value;
            }
        }
        #endregion

        #region Constructor
        public THSComboBox()
        {
            InitializeComponent();
        }

        public THSComboBox(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }
        #endregion

        #region--Initialize--
        public void InitializeControl()
        {
            InitObjectDataToComboBox();
            if (!String.IsNullOrEmpty(THSDataSource) && !String.IsNullOrEmpty(THSDataMember))
            {
                if (!THSDataSource.Equals("ADConfigValues"))
                    Screen.BindingDataControl(this);
            }

            //Set Edit Value of Combo Box is String.Empty if FieldTag is SC
            if (Tag.ToString() == THSERPScreen.SearchControl)
            {
                this.EditValue = String.Empty;
            }
            this.Click += new EventHandler(((BaseModuleERP)Screen.Module).Control_Click);
            this.KeyUp += new KeyEventHandler(((BaseModuleERP)Screen.Module).Control_KeyUp);
        }

        private void InitObjectDataToComboBox()
        {
            //ComboBox is MatchCode
            if (THSDataMember.Contains("MatchCode"))
            {
                InitMatchCodeValueToComboBox(THSDataMember, Tag.ToString());
            }
            else if ((THSDataSource.Contains("ADConfigValues")) && !String.IsNullOrEmpty(THSDataMember))
            {
                InitComboBoxFromADConfigValues();
            }
        }

        private void InitMatchCodeValueToComboBox(String strMatchCodeColumnName, String strFieldTag)
        {
            this.Properties.Items.Clear();
            ADMatchCodesController objADMatchCodesController = new ADMatchCodesController();
            DataSet dsMatchCodeValue = objADMatchCodesController.GetADMatchCodesByADMatchCodeColumnName(strMatchCodeColumnName);
            if (dsMatchCodeValue.Tables.Count > 0)
            {
                if (dsMatchCodeValue.Tables[0].Rows.Count > 0)
                {
                    if (strFieldTag == "SC")
                        this.Properties.Items.Add("");
                    foreach (DataRow row in dsMatchCodeValue.Tables[0].Rows)
                    {
                        ADMatchCodesInfo objADMatchCodesInfo = (ADMatchCodesInfo)objADMatchCodesController.GetObjectFromDataRow(row);
                        this.Properties.Items.Add(objADMatchCodesInfo.ADMatchCodeValue);
                    }
                    if (this.Properties.Items.Count > 0)
                        this.SelectedIndex = 0;
                }
                else
                {
                    this.Properties.Buttons.Clear();
                }
            }
            dsMatchCodeValue.Dispose();
        }

        private void InitComboBoxFromADConfigValues()
        {
            if (Tag.ToString() == "SC")
                this.Properties.Items.Add("");
            String strConfigValueGroup = String.Empty;
            if (THSDataMember.Contains("Combo"))
                strConfigValueGroup = THSDataMember.Substring(2, THSDataMember.Length - 7);
            else
                strConfigValueGroup = THSDataMember.Substring(2, THSDataMember.Length - 2);
            if (ADConfigValueUtility.glbdsConfigValues.Tables[strConfigValueGroup] != null)
            {
                foreach (DataRow row in ADConfigValueUtility.glbdsConfigValues.Tables[strConfigValueGroup].Rows)
                {
                    this.Properties.Items.Add(row["Text"].ToString());
                }
            }
            if (this.Properties.Items.Count > 0)
                this.SelectedIndex = 0;
            else
                this.Text = String.Empty;
        }
        #endregion
    }
}
